package com.javaAdmin.controller;

import com.javaAdmin.mapper.StudentMapper;
import com.javaAdmin.model.Student;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author Administrator
 */
// RestController和controller区别主要有以下：
// 1.用Controller配合视图解析器才能返回到指定页面。在对应的方法上加上ResponseBody注解才能返回JSON，XML或自定义mediaType的内容到页面。
// 2.不可以只用RestController注解Controller，因为这样会让Controller中的内容不能返回jsp页面，而且会直接返回Return里的内容。
// 3.RestController相当于Controller和ResponseBod两者合并起来的作用。

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private HttpServletRequest request;


    @PostMapping("/save")
    public Student save(Student student) {
        return student;
    }

    @PostMapping("/save/json")
    public Student saveJson(@RequestBody Student student) {
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());

        studentMapper.save(student);
        System.out.println(student.getId());

        return student;
    }

    @GetMapping("/get")
    public Student getInfo(Student student) {
        return student;
    }


    @GetMapping("/get/{sex}/{age}")
    public Student getByUrl(@PathVariable Integer age, @PathVariable Integer sex) {
        Student student = new Student();
        student.setAge(age);
        student.setSex(sex);
        return student;
    }

    @GetMapping("/getParam")
    public Student getByRequestParam(@RequestParam("name") String name, @RequestParam("sex") Integer sex) {
        Student student = new Student();
        student.setName(name);
        student.setSex(sex);
        return student;
    }

    @GetMapping("/getRequest")
    public Student getRequest() {
        String name = request.getParameter("name");
        Integer age = Integer.valueOf(request.getParameter("age"));
        Integer sex = Integer.valueOf(request.getParameter("sex"));

        Student student = new Student();
        student.setName(name);
        student.setAge(age);
        student.setSex(sex);
        return student;
    }
}
